const Mock = require('mockjs')

const tokens = {
    admin: {
        token: 'admin-token'
    },
    editor: {
        token: 'editor-token'
    }
}

const users = {
    'admin-token': {
        roles: ['admin'],
        introduction: 'I am a super administrator',
        avatar:
            'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: 'Super Admin'
    },
    'editor-token': {
        roles: ['editor'],
        introduction: 'I am an editor',
        avatar:
            'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: 'Normal Editor'
    }
}

const userList = []
const count = 50

userList.push({
    id: 0,
    username: 'admin',
    realname: 'tudou',
    email: 'le.z@foxmail.com',
    mobile: '13545431530',
    role: 0,
    status: true,
    lastLoginTime: '2021-02-02 02:02:02',
    createTime: '2021-02-02 02:02:02'
})

const userLength = userList.length

for (let i = userLength; i <= count - userLength; i++) {
    userList.push(
        Mock.mock({
            id: i,
            username: '@string("lower", 5, 10)',
            realname: '@cname',
            email: '@email',
            mobile: /1(3|5|7|8)\d{9}/,
            role: 1,
            'status|1': [true, false],
            lastLoginTime: '@datetime',
            createTime: '@datetime'
        })
    )
}

// console.log(userList)

module.exports = [
    // user login
    {
        url: '/api/user/login',
        type: 'post',
        response: config => {
            const { username } = config.body
            const token = tokens[username]

            // mock error
            if (!token) {
                return {
                    code: 60204,
                    message: 'Account and password are incorrect.'
                }
            }

            return {
                code: 20000,
                data: token
            }
        }
    },
    {
        url: '/api/user/list',
        type: 'get',
        response: config => {
            console.log(config.query)
            const {
                username,
                realname,
                email,
                mobile,
                role,
                status,
                pagenum = 1,
                pagesize = 10,
                sort
            } = config.query

            console.log(pagenum)
            console.log(pagesize)

            let mockuserList = userList.filter(item => {
                if (status && item.status !== status) return false
                if (role && item.role !== role) return false
                if (username && item.username.indexOf(username) < 0)
                    return false
                if (realname && item.realname.indexOf(realname) < 0)
                    return false
                if (email && item.email.indexOf(email) < 0) return false
                if (mobile && item.mobile.indexOf(mobile) < 0) return false
                return true
            })

            if (sort === '-id') {
                mockuserList = mockuserList.reverse()
            }

            const pageuserList = mockuserList.filter(
                (item, index) =>
                    index < pagesize * pagenum &&
                    index >= pagesize * (pagenum - 1)
            )

            return {
                code: 20000,
                data: {
                    total: mockuserList.length,
                    items: pageuserList
                }
            }
        }
    },
    // get user info
    {
        url: '/api/user/info.*',
        type: 'get',
        response: config => {
            const { token } = config.query
            const info = users[token]

            // mock error
            if (!info) {
                return {
                    code: 50008,
                    message: 'Login failed, unable to get user details.'
                }
            }

            return {
                code: 20000,
                data: info
            }
        }
    },

    // user logout
    {
        url: '/api/user/logout',
        type: 'post',
        response: _ => {
            return {
                code: 20000,
                data: 'success'
            }
        }
    }
]
